package com.erp.controller.warehouse;

import com.alibaba.excel.EasyExcel;
import com.erp.common.annotation.Anonymous;
import com.erp.common.annotation.Log;
import com.erp.common.core.controller.BaseController;
import com.erp.common.core.domain.AjaxResult;
import com.erp.common.core.page.TableDataInfo;
import com.erp.common.enums.BusinessType;
import com.erp.domain.ErpWarehouse;
import com.erp.service.warehouse.IWarehouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

/**
 * @Author cuicui
 * @Description
 * @Date 2025-9-2
 */
@RestController
@RequestMapping("/erp/warehouse")
public class WarehouseController extends BaseController {
    @Autowired
    private IWarehouseService warehouseService;

    // 获取仓库信息列表
    @GetMapping("/list")
    @Anonymous
    public TableDataInfo ListWarehouse(ErpWarehouse erpWarehouse, @RequestParam Map<String, Object> params) {

        erpWarehouse.getParams().putAll(params);
        startPage();
        List<ErpWarehouse> list = warehouseService.ListWarehouse(erpWarehouse);
        System.out.println("这是打印信息" + list);
        return getDataTable(list);
    }

    /**
     * 新增仓库信息
     */
    // @PreAuthorize("@ss.hasPermi('system:erpWarehouse:add')")
    // @Log(title = "仓库信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody ErpWarehouse erpWarehouse) {
        return toAjax(warehouseService.add(erpWarehouse));
    }

    /**
     * 获取仓库信息详细信息
     */
    // @PreAuthorize("@ss.hasPermi('system:warehouse:query')")
    // @GetMapping(value = "/{warehouseId}")
    @GetMapping("/getInfo/{warehouseId}")
    public AjaxResult getInfo(@PathVariable("warehouseId") Long warehouseId) {
        ErpWarehouse erpWarehouse = warehouseService.getInfo(warehouseId);
        return success(erpWarehouse);
    }

    // 修改仓库信息
    @PutMapping("/update")
    public AjaxResult update(@RequestBody ErpWarehouse erpWarehouse) {
        return toAjax(warehouseService.updateWarehouse(erpWarehouse));
    }

    /**
     * 导入单位
     */
    @PostMapping("/import")
    @Log(title = "仓库信息", businessType = BusinessType.INSERT)
    public AjaxResult importUnit(@RequestParam("file") MultipartFile file) {
        try {
            // 重点,此处第三个参数需要传入Service对象
            // EasyExcel.read(file.getInputStream( ),
            //                 ErpWarehouse.class,
            //                 new MdUnitMeasureDataListener(mdUnitMeasureService))
            //         .sheet( ).doRead( );
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success();
    }

    /**
     * 导出单位
     */
    @GetMapping("/export")
    @Log(title = "仓库信息", businessType = BusinessType.EXPORT)
    public void exportUnit(HttpServletResponse response) throws Exception {

        // 调用业务层
        List<ErpWarehouse> list = warehouseService.ListWarehouse(new ErpWarehouse());

        // 导出.xls用这个
        // response.setContentType("application/vnd.ms-excel");
        // 导出.xlsx用这个
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码
        String fileName = URLEncoder.encode("仓库信息", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");

        // easyexcel导出工具
        EasyExcel.write(response.getOutputStream(), ErpWarehouse.class).autoCloseStream(Boolean.FALSE).sheet("仓库信息").doWrite(list);
    }

}
